package com.hry.sunflower.images.common.response;

import com.hry.sunflower.images.common.constants.CommonConstants;
import lombok.Data;

import java.io.Serializable;

/**
 * @Desc
 * @Author houry
 * @Date 2021/1/23 16:23
 **/
@Data
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 响应码
     */
    private int code;

    /**
     * 响应信息
     */
    private String msg;

    /**
     * 响应数据
     */
    private T data;

    public static <T> Result<T> SUCCESS() {
        return restResult(null, CommonConstants.SUCCESS, "操作成功！");
    }

    public static <T> Result<T> SUCCESS(T data) {
        return restResult(data, CommonConstants.SUCCESS, "操作成功！");
    }

    public static <T> Result<T> SUCCESS(T data, String msg) {
        return restResult(data, CommonConstants.SUCCESS, msg);
    }

    public static <T> Result<T> FAIL() {
        return restResult(null, CommonConstants.FAIL, "操作失败！");
    }

    public static <T> Result<T> FAIL(String msg) {
        return restResult(null, CommonConstants.FAIL, msg);
    }

    public static <T> Result<T> FAIL(T data) {
        return restResult(data, CommonConstants.FAIL, "操作失败！");
    }

    public static <T> Result<T> FAIL(T data, String msg) {
        return restResult(data, CommonConstants.FAIL, msg);
    }


    private static <T> Result<T> restResult(T data, int code, String msg) {
        Result<T> apiResult = new Result();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

}
